import sun.reflect.generics.tree.Tree;

import java.util.Stack;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        //构造方法
        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode root;
    //创建二叉树，返回根节点
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
        /*
        *            A
        *           / \
        *          B   C
        *         /\   /\
        *        D  E F  G
        *           \
        *            H
        * */
    }

    //前序遍历(非递归)
    //利用栈
    public void prevOrderNor (TreeNode root) {
        if (root == null) {
            return;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                //先将当前遍历节点入栈
                stack.push(cur);
                System.out.print(cur.val + " ");//打印
                cur = cur.left;
            }

            //弹出栈顶元素之后遍历其右边
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    //中序遍历(非递归)
    public void inOrderNor(TreeNode root) {
        //和前序遍历是一样的，只是打印顺序不同
        if (root == null) {
            return;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        //此时就是弹出时打印
        while (!stack.isEmpty() || cur != null) {
            //先入栈
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            //此时cur走到空
            cur = top.right;
        }
    }

    //后序遍历(非递归)
    public void postOrderNor(TreeNode root) {
        if (root == null) {
            return;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;//此时要多定义一个以便来记录根节点

        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }

            //此时我们先不进行删除,我们先来看栈顶元素
            TreeNode top = stack.peek();

            //判断cur.right 是否为空
            //并且判断 prev == top
            if (top.right == null || top.right == prev) {
                //打印栈顶元素
                System.out.print(top.val + " ");
                //删除栈顶元素
                stack.pop();
                prev = top;
                //记录最新被打印的节点
            } else {
                cur = top.right;
            }
        }
    }

}
